package beegoRoute

import (
	"github.com/beego/beego/v2/server/web"
	"net/http"
)

// GroupServer 使用map 解决路径重复问题
type GroupServer struct {
	groupPath string
	routeMap  map[string]*Route
	handle    *struct {
		method  string
		handler web.HandleFunc
	}
}

func NewGroupServer(groupPath string) *GroupServer {
	return &GroupServer{groupPath: groupPath, routeMap: make(map[string]*Route), handle: &struct {
		method  string
		handler web.HandleFunc
	}{method: http.MethodGet}}
}
func (r *GroupServer) AddGroupHandle(method string, handler web.HandleFunc) *GroupServer {
	r.handle.method = method
	r.handle.handler = handler
	return r
}
func (r *GroupServer) AddRouteList(routes ...*Route) *GroupServer {
	for _, route := range routes {
		r.routeMap[route.path] = route
	}
	return r
}

// DelRoute  删除
func (r *GroupServer) DelRoute(path string) bool {
	maps := r.routeMap
	if _, ok := maps[path]; ok {
		delete(maps, path)
		return true
	}
	return false
}
